package com.yw.smartcityforjava.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.yw.smartcityforjava.App;
import com.yw.smartcityforjava.R;
import com.yw.smartcityforjava.activity.MainActivity;
import com.yw.smartcityforjava.activity.NewsDetailActivity;
import com.yw.smartcityforjava.adapter.ServiceGridAdapter;
import com.yw.smartcityforjava.adapter.NewsListAdapter;
import com.yw.smartcityforjava.api.ICallback;
import com.yw.smartcityforjava.api.ServiceCreator;
import com.yw.smartcityforjava.api.allservices.ServicesResponse;
import com.yw.smartcityforjava.api.homeBanner.HomeBannerResponse;
import com.yw.smartcityforjava.api.news.NewsListRes;
import com.yw.smartcityforjava.api.news.NewsTypeResponse;
import com.yw.smartcityforjava.view.MyPageAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class HomeFragment extends Fragment {

    private Banner banner;
    private GridView gridview;
    private TabLayout tabLayout;
//    private ListView newsLv;
    private ViewPager newsVp;
    private NewsListAdapter newsListAdapter;
    private LayoutInflater layoutInflater;
    private List<View> newsListViews;
    private List<String> listTab;
    private MyPageAdapter myPageAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        banner = (Banner) root.findViewById(R.id.banner);
        gridview = (GridView) root.findViewById(R.id.gridview);
        tabLayout = (TabLayout) root.findViewById(R.id.tabview);
        newsVp = (ViewPager)root.findViewById(R.id.newsVp);
        //newsLv = (ListView) root.findViewById(R.id.newsList);
        Toolbar toolbar = root.findViewById(R.id.toolbar);
        TextView titleTv = toolbar.findViewById(R.id.titleTv);
        titleTv.setText("首页");
        //获取LayoutInflater
        layoutInflater = getLayoutInflater();
        newsListViews = new ArrayList<>();
        listTab= new ArrayList<>();
        myPageAdapter =  new MyPageAdapter(newsListViews,listTab, getContext());
        newsVp.setAdapter(myPageAdapter);

        loadBannerData();
        //加载全部服务
        loadServices();
        //加载新闻列表类型
        loadNewsTypes();

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                loadNewsList((Integer) tab.getTag());
                myPageAdapter.setListView(newsListViews);
                myPageAdapter.setListTab(listTab);
                myPageAdapter.notifyDataSetChanged();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position == 7){
                    //getActivity()
                    ((MainActivity)getActivity()).setNavigationSelectedItemId(R.id.service);
                }else{

                }
            }
        });
//        newsLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                NewsListRes.RowsBean data = (NewsListRes.RowsBean) newsListAdapter.getItem(position);
//                Intent intent = new Intent(getContext(), NewsDetailActivity.class);
//                intent.putExtra("title", data.getTitle());
//                intent.putExtra("content", data.getContent());
//                intent.putExtra("img", data.getCover());
//                intent.putExtra("commentNum", data.getCommentNum());
//                intent.putExtra("publicTime", data.getCreateTime());
//
//                startActivity(intent);
//            }
//        });


        return root;
    }

    private void loadNewsList(int newsType){
        ServiceCreator.getInstance().getApi().loadNewsList(newsType).enqueue(new ICallback<NewsListRes>() {
            @Override
            public void onSuccess(NewsListRes response) {

                //创建vp的adapter


                newsListAdapter = new NewsListAdapter();
                newsListAdapter.setDatas(response.getRows());
                //newsLv.setAdapter(newsListAdapter);
            }
        });
    }

    private void loadNewsTypes() {
        ServiceCreator.getInstance().getApi().loadNewsTypeList().enqueue(new ICallback<NewsTypeResponse>() {
            @Override
            public void onSuccess(NewsTypeResponse response) {
                List<NewsTypeResponse.DataBean> types = response.getData();
                types.forEach(new Consumer<NewsTypeResponse.DataBean>() {
                    @Override
                    public void accept(NewsTypeResponse.DataBean dataBean) {
                        TabLayout.Tab tab = tabLayout.newTab();
                        tab.setTag(dataBean.getId());
                        tab.setText(dataBean.getName());
                        tabLayout.addTab(tab);

                        //动态加载View的布局文件
                        View view = layoutInflater.inflate(R.layout.news_list_layout,null);
                        newsListViews.add(view);
                        listTab.add(dataBean.getName());
                    }
                });
            }
        });
    }

    private void loadBannerData() {
        ServiceCreator.getInstance().getApi().loadHomeBanner().enqueue(new ICallback<HomeBannerResponse>() {
            @Override
            public void onSuccess(HomeBannerResponse response) {

                List<HomeBannerResponse.RowsBean> datas = response.getRows();
                banner.setAdapter(new BannerImageAdapter<HomeBannerResponse.RowsBean>(datas) {
                    @Override
                    public void onBindView(BannerImageHolder bannerImageHolder, HomeBannerResponse.RowsBean rowsBean, int i, int i1) {
                        Glide.with(requireContext()).load(App.baseUrl + rowsBean.getAdvImg()).into(bannerImageHolder.imageView);
                        bannerImageHolder.imageView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(App.app, "跳转到详情页", Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                });
                banner.setIndicator(new CircleIndicator(requireContext()));
                banner.start();
            }
        });
    }
    private void loadServices(){
        ServiceCreator.getInstance().getApi().loadAllServices().enqueue(new ICallback<ServicesResponse>() {
            @Override
            public void onSuccess(ServicesResponse response) {
                List<ServicesResponse.RowsBean> datas = response.getRows();
                Log.d("services", String.valueOf(datas.size()));

                ServicesResponse.RowsBean more = new ServicesResponse.RowsBean();
                more.setServiceName("更多服务");

                List<ServicesResponse.RowsBean> newDatas = new ArrayList<>();
                for(int i=0;i<datas.size();i++){
                    if(i<7){
                        newDatas.add(datas.get(i));
                    }else{
                        break;
                    }
                }
                newDatas.add(more);
                ServiceGridAdapter adapter = new ServiceGridAdapter();
                adapter.setDatas(newDatas);
                gridview.setAdapter(adapter);
            }
        });
    }
}